<template>
  <div class="bg">
    <div class="login">
      <div class="logo">
        <img :src="logo" alt="图标">
        <h1>光储直柔能源管理平台</h1>
      </div>
      <div class="login-form" >
        <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef">
          <el-form-item prop="username">
            <el-input placeholder="请输入用户名" prefix-icon="User" v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" prefix-icon="Lock" type="password" v-model="ruleForm.password"> </el-input>
          </el-form-item>
          <el-form-item >  
            <el-button type="primary" style="width:100%" @click="handleClick()">登录</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
  </div>


</template>

<script setup lang="ts">
import logo from '@/assets/logo.png'
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { useUserStore } from '@/store/auth'
import { useRouter } from 'vue-router'
interface RulesForm {
  username: string,
  password: string,
}

let ruleForm:RulesForm = reactive({
  username: '',
  password: '',
})

const rules = reactive<FormRules<RulesForm>>({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { min: 4, max: 8, message: '用户名长度应该是4-8位', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
  ],
})

//导入仓库
const userStore = useUserStore()
//导入路由器
const router = useRouter()

const ruleFormRef = ref<FormInstance>();
const handleClick = () => {
  //？.是可选链操作符，validata是elements-plus的表单验证方法，判断用户输入是否符合规则
  ruleFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      {
        await userStore.login(ruleForm)
        router.push('/')
      }
    } 
})
}
</script>


<style scoped lang="less">
.bg {
  background-image: url('@/assets/bg.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 100vh;
  width: 100vw;

  .login {
    width: 500px;
    height: 300px;
    padding: 50px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    position: absolute;
    top: 50%;
    margin-top: -200px;
    margin-left: 10%;



    .logo {
      display: flex;
      align-items: center;
      justify-content: center;



      img {
        width: 80px;
        height: 80px;
      }

      h1 {
        color: rgb(15, 53, 147);
        font-size: 35px;
        font-weight: bold;
        margin-left: 30px;
      }
    }
  }

  .login-form {
    margin-top: 40px;

  }

}
</style>